package com.rich.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailByQQ {
	public static void sendEmail(String to ,

			String content ) throws Exception {

			              /*

			               * 1、创建连接对象

			               * 2、创建邮件对象

			               * 3、发送邮件

			               */

//			            1、创建连接对象

			              Properties props = new Properties();

			              props.put("mail.transport.protocol" , "smtp");

			              props.put("mail.smtp.host", "smtp.qq.com");

			              props.put("mail.smtp.auth", "true");

			              props.put("mail.smtp.port", 465);

			              props.put("mail.smtp.ssl.enable", "true");

			              Session session=Session.getInstance(props, new Authenticator() {

			                     protected PasswordAuthentication getPasswordAuthentication(){//认证信息

			                            return new PasswordAuthentication("960441287@qq.com", "yqwrmaovckjqbcia");

			                     }

			              });


//			            2、创建邮件对象

			              Message message = new MimeMessage(session);

			              message.setFrom(new InternetAddress("960441287@qq.com"));

			              message.setRecipient(RecipientType.TO, new InternetAddress(to));

			              //message.setRecipient(RecipientType.CC, new InternetAddress("2047368612@qq.com"));

			              message.setSubject("主题");

			              message.setContent(content, "text/html;charset=utf-8");


//			            3、发送邮件

			              Transport.send(message);

			       }
	public static void main(String[] args) {
		try {
			MailByQQ.sendEmail("1510440019@qq.com", "sa");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
